<?php

namespace app\admin\controller;

use app\service\MsgService;
use think\Controller;
use app\common\model\Manager as ManagerModel;
use app\common\model\User;
//use vaptcha\Vaptcha;
use think\Loader;
use GuzzleHttp\Client;
use app\common\model\Config;
use vaptcha\Vaptcha;


class Login extends Controller
{

    public function __construct()
    {
        Loader::import("vaptcha.vaptcha",EXTEND_PATH,'.class.php');
        parent::__construct();
        config(Config::getConfig());
    }

    public function index()
    {
        $this->assign('data', [
            'type' => 'click',  //展示方式，默认为嵌入式
            'vid' => config('vaptcha_id')
        ]);
        return view();
    }

    public function test(){
        //配置模板消息
        MsgService::S([
            'mall_id' => 1
        ])->resetMallTmpl(1);
    }

    public function dologin()
    {
        $input = input();

//        if (!$input['token']) {
//            return returnJson(1, '请填写token');
//        };

//        $client = new Client([]);
//        $response = $client->post('http://api.vaptcha.com/v2/validate', [
//            'form_params' => [
//                'id' => config('vaptcha_id'),
//                'secretkey' => config('vaptcha_key'),
//                'scene' => '',
//                'token' => $input['token'],
//                'ip' => ''
//            ]
//        ]);
//        $response = $client->post('http://0.vaptcha.com/verify', [
//            'form_params' => [
//                'id' => config('vaptcha_id'),
//                'secretkey' => config('vaptcha_key'),
//                'scene' => $input['scene'],
//                'token' => $input['token'],
//                'ip' => ''
//            ]
//        ]);

//        $body = $response->getBody();
//        $result = json_decode((string)$body, true);
//        if ($result['success'] == 0) {
//            return returnJson(1, '验证码错误');
//        }

        if (!$input['name'] || !$input['password']) {
            return returnJson(1, '请填写用户名和密码');
        }

        $manager = ManagerModel::getOne([
            'name' => $input['name']
        ]);

        if (!$manager || createPassword($input['password']) != $manager['password']) {
            return returnJson(1, '密码错误');
        }


        session('manager', $manager);
        return returnJson(0, '登陆成功', '/w');
    }

    /**
     * 验证
     */
    public function vaptcha()
    {
        if (isset($_REQUEST['offline_action'])) {
            if (isset($_GET['v'])) {
                echo Vaptcha::offline($_GET['offline_action'], $_GET['callback'], $_GET['v'], $_GET['knock']);
            } else {
                echo Vaptcha::offline($_GET['offline_action'], $_GET['callback']);
            }
        } else {
            if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                $token = $_POST['token'];
                $scene = $_POST['scene'];
                $result = json_decode(Vaptcha::validate($token, $scene),true);
                return $result;
                if ($result['success'] == 0) {
                    return returnJson(1, '验证码错误');
                }
                print_r($result);
            }
        }
    }


    public function quit()
    {
        session('manager', null);
        return returnJson(2, '已退出', '/w');
    }

}
